﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;

using Willson.JsbManager.Idal;
using Willson.JsbManager.Entity;
using Willson.JsbManager.BllEntity;
using Willson.JsbManager.Comm;


namespace Willson.JsbManager.Bll
{
    /// <summary>
    /// 系统菜单信息管理类
    /// </summary>
    public class BLLMenuInfo:BaseBLL
    {
        private ITableXtMenu xt_menu_DAL = null;
        private ITableXtUserMenu xt_user_menu_DAL = null;

        private int m_user_id;

        public BLLMenuInfo()
        {
            xt_menu_DAL = Reflect<ITableXtMenu>.Create("DAL_table_xt_menu", "Willson.JsbManager.Dal");
            xt_user_menu_DAL = Reflect<ITableXtUserMenu>.Create("DAL_table_xt_user_menu", "Willson.JsbManager.Dal");
        }

        public List<BEChildMenuInfo> GetQuickMenuList(int m_user_id)
        {
            List<BEChildMenuInfo> list = new List<BEChildMenuInfo>();
            Hashtable hash = new Hashtable();
            hash.Add("user_id", m_user_id);
            hash.Add("menu_quick", 1);

            IEnumerator<Table_xt_user_menu_entity> ator = xt_user_menu_DAL.Find(hash, SqlOperator.And, true).GetEnumerator();

            while (ator.MoveNext())
            {
                BEChildMenuInfo info = new BEChildMenuInfo();
                Table_xt_menu_entity menu = xt_menu_DAL.FindByID(ator.Current.Menu_id);

                info.Id = menu.Id;
                info.MenuBmp = menu.Menu_bmp;
                info.MenuIsshow = menu.Menu_isshow;
                info.MenuMemo = menu.Menu_memo;
                info.MenuName = menu.Menu_name;
                info.MenuWindow = menu.Menu_window;

                list.Add(info);
            }

            return list;
        }

        
        public void MakeQuickFlag(int m_user_id, int m_menu_id, int flag)
        {
            Hashtable recordField = new Hashtable();
            Hashtable recordWhere = new Hashtable();

            recordField.Add("menu_quick", flag);
            recordWhere.Add("user_id", m_user_id);
            recordWhere.Add("menu_id", m_menu_id);

            xt_user_menu_DAL.Update(recordField, recordWhere);
        }
        
        /// <summary>
        /// 查询某个菜单是否建有快捷方式
        /// </summary>
        /// <param name="m_usre_id"></param>
        /// <param name="m_menu_id"></param>
        /// <returns></returns>
        public Boolean QueryMenuQuickFlag(int m_usre_id, int m_menu_id)
        {
            Hashtable hash = new Hashtable();
            hash.Add("user_id",m_usre_id);
            hash.Add("menu_id", m_menu_id);

            Table_xt_user_menu_entity info = xt_user_menu_DAL.Find(hash);

            if (info == null) return false;

            if (info.Menu_quick == 0)
                return false;
            else
                return true;

        }

        public string GetMenuItemName(int menu_id)
        {
            Table_xt_menu_entity info = xt_menu_DAL.FindByID(menu_id);

            return info.Menu_name;
        }

        public string GetParentMenuName(int menu_id)
        {
            Table_xt_menu_entity info = xt_menu_DAL.FindByID(menu_id);

            Table_xt_menu_entity parent = xt_menu_DAL.FindByID(info.Menu_parent);

            return parent.Menu_name;
        }

        /// <summary>
        /// 得到子菜单信息
        /// </summary>
        /// <param name="parent_menu_id">父菜单ID</param>
        /// <returns>子菜单信息</returns>
        public List<BEChildMenuInfo> GetChildMenuInfo(int parent_menu_id)
        {
            List<BEChildMenuInfo> list = new List<BEChildMenuInfo>();
            BEChildMenuInfo info = null;
            Hashtable ht = new Hashtable();

            ht.Add("menu_parent", parent_menu_id);

            IEnumerator<Table_xt_menu_entity> ator = xt_menu_DAL.Find(ht,SqlOperator.And,true).GetEnumerator();

            while (ator.MoveNext())
            {
                info = new BEChildMenuInfo();

                info.Id = ator.Current.Id;
                info.MenuBmp = ator.Current.Menu_bmp;
                info.MenuMemo = ator.Current.Menu_memo;
                info.MenuName = ator.Current.Menu_name;
                info.MenuWindow = ator.Current.Menu_window;
                info.MenuIsshow = ator.Current.Menu_isshow;

                list.Add(info);
            }

            return list;
        }


        /// <summary>
        /// 得到系统父级菜单信息
        /// </summary>
        /// <param name="user_id">用户ID</param>
        /// <returns>父级菜单信息</returns>
        public List<BEParentMenuInfo> GetParentMenuInfo(int user_id)
        {
            this.m_user_id = user_id;
            List<BEParentMenuInfo> list = new List<BEParentMenuInfo>();
            BEParentMenuInfo info = null;
            Hashtable ht = new Hashtable();

            ht.Add("user_id", user_id);

            IEnumerator<Table_xt_user_menu_entity> ator = xt_user_menu_DAL.Find(ht, SqlOperator.And,true).GetEnumerator();

            while (ator.MoveNext())
            {
                Hashtable tmp = new Hashtable();
                tmp.Add("id", ator.Current.Menu_id);

                IEnumerator<Table_xt_menu_entity> ator2 = xt_menu_DAL.Find(tmp,SqlOperator.And,true).GetEnumerator();
                
                while(ator2.MoveNext())
                {
                    if (ator2.Current.Menu_parent == 0)
                    {
                        info = new BEParentMenuInfo();
                        info.Menu_id = ator2.Current.Id;
                        info.Menu_name = ator2.Current.Menu_name;

                        list.Add(info);
                    }
                }

            }

            return list;
        }
    }
}
